package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.GradeBean;
import com.project.bean.LogBean;
import com.project.bean.RoleBean;
import com.project.bean.UserBean;
import com.project.mapper.IGradeMapper;
import com.project.mapper.ILogMapper;
import com.project.mapper.IRoleMapper;
import com.project.mapper.IUserMapper;
import com.project.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
 * @Title: UserServiceImpl
 * @Package: com.project.service.impl
 * @Description:
 * @Author: Liang-Xie
 */
@SuppressWarnings("all")
@Service
@Transactional
public class UserServiceImpl implements IUserService {
    @Autowired
    private IUserMapper userMapper;
    @Autowired
    private ILogMapper logMapper;
    @Autowired
    private IGradeMapper gradeMapper;
    @Autowired
    private IRoleMapper roleMapper;

    @Override
    public UserBean login(String name, String pwd) {
        UserBean userBean = null;
        QueryWrapper<UserBean> qw = new QueryWrapper<>();
        qw.eq("u_userName", name);
        qw.eq("u_pwd", pwd);
        userBean = userMapper.selectOne(qw);
        if (userBean != null) {
            logMapper.insert(new LogBean("用户" + userBean.getRealName() + "登录系统"));
            userBean = this.findById(userBean.getId());
        }
        return userBean;
    }

    @Override
    public IPage<UserBean> findByItem(Integer pageNO, String name) {
        QueryWrapper<UserBean> qw = new QueryWrapper<>();
        if (name != null && name.length() != 0) {
            qw.like("u_userName", name);
        }

        return userMapper.selectPage(new Page<>(pageNO, 5), qw);
    }

    @Override
    public boolean add(UserBean user, Integer[] roleIdArray) {
        QueryWrapper<UserBean> qw = new QueryWrapper<>();
        qw.eq("u_userName", user.getUserName());
        UserBean newUser = userMapper.selectOne(qw);
        if (newUser == null) {
            userMapper.insert(user);
            userMapper.add(user.getId(), roleIdArray);
            return true;
        }
        return false;
    }

    @Override
    public void del(Integer userId) {
        userMapper.del(userId);
    }

    @Override
    public void updateRole(Integer userId, Integer[] roleIdArray) {
        userMapper.updateRole(userId, roleIdArray);
    }

    @Override
    public UserBean findById(Integer userId) {
        UserBean user = userMapper.selectById(userId);

        Set<RoleBean> roleBeanSet = new HashSet<>();
        List<RoleBean> roleBeanList = roleMapper.findByUserId(userId);
        roleBeanSet.addAll(roleBeanList);
        user.setRoleSet(roleBeanSet);

        Set<GradeBean> gradeBeanSet = new HashSet<>();
        for (RoleBean roleBean : roleBeanList) {
            gradeBeanSet.addAll(gradeMapper.findByRoleId(roleBean.getId()));
        }
        user.setGradeSet(gradeBeanSet);
        return user;
    }

    @Override
    public void updatePwd(Integer userId, String pwd) {
        UserBean user = userMapper.selectById(userId);
        user.setPassword(pwd);
        userMapper.updateById(user);
    }
}
